package com.chenfan.process.util;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * @author liuming
 * @date 2021/11/30
 */
public class MathUtil {

    public static boolean calculate(Integer relatedType, Integer formAttributeType, String source, String target) {
        boolean flag = false;

        if (formAttributeType == 1) {
            switch (relatedType) {
                case 1:
                    flag = greaterThan(source, target);
                    break;
                case 2:
                    flag = greaterEqualsThan(source, target);
                    break;
                case 3:
                    flag = lessThan(source, target);
                    break;
                case 4:
                    flag = lessEqualsThan(source, target);
                    break;
                case 5:
                    flag = equals(source, target);
            }
        }
        // 字符串类型
        if (formAttributeType == 2) {
            switch (relatedType) {
                case 1:
                    flag = stringEquals(source, target);
                    break;
                case 2:
                    flag = stringNotEquals(source, target);
            }
        }
        return flag;
    }

    /**
     * 大于
     *
     * @param source
     *            源
     * @param target
     *            目标
     * @return boolean
     */
    public static boolean greaterThan(String source, String target) {
        return new BigDecimal(source).compareTo(new BigDecimal(target)) > 0;
    }

    /**
     * 大于等于
     *
     * @param source
     *            源
     * @param target
     *            目标
     * @return boolean
     */
    public static boolean greaterEqualsThan(String source, String target) {
        return new BigDecimal(source).compareTo(new BigDecimal(target)) >= 0;
    }

    /**
     * 小于
     *
     * @param source
     *            源
     * @param target
     *            目标
     * @return boolean
     */
    public static boolean lessThan(String source, String target) {
        return new BigDecimal(source).compareTo(new BigDecimal(target)) < 0;
    }

    /**
     * 小于等于
     *
     * @param source
     *            源
     * @param target
     *            目标
     * @return boolean
     */
    public static boolean lessEqualsThan(String source, String target) {
        return new BigDecimal(source).compareTo(new BigDecimal(target)) <= 0;
    }

    /**
     * 等于
     *
     * @param source
     *            源
     * @param target
     *            目标
     * @return boolean
     */
    public static boolean equals(String source, String target) {
        return new BigDecimal(source).compareTo(new BigDecimal(target)) == 0;
    }

    /**
     * 不等于
     * 
     * @param source
     *            源
     * @param target
     *            目标
     * @return boolean
     */
    public static boolean stringNotEquals(String source, String target) {
        return !equals(source, target);
    }

    /**
     * 字符串等于
     *
     * @param source
     *            源
     * @param target
     *            目标
     * @return boolean
     */
    public static boolean stringEquals(String source, String target) {
        return Objects.equals(source, target);
    }

    public static boolean isNumeric(String source) {
        return source.matches("([1-9]+[0-9]*|0)(\\.[\\d]+)?");
    }
}
